﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace CoderJoe.WoW.GearManager.Views
{
	public partial class CharacterView : UserControl
	{
		private List<EquipmentButton> ButtonList { get; set; }
		protected CharacterController Controller { get; private set; }
		protected IEnumerable<EquipmentButton> EquipmentButtons { get { return ButtonList.AsEnumerable(); } }

		public CharacterView(CharacterController controller)
		{
            //ButtonList = new List<EquipmentButton>();

			Controller = controller;

			InitializeComponent();

            ButtonList = 
                (from c in this.tableLayoutPanel1.Controls.OfType<EquipmentButton>()
                 orderby c.EquipmentSlotId
                 select c).ToList();

            //foreach (Control c in this.tableLayoutPanel1.Controls.OfType<EquipmentButton
            //{
            //    if (c is EquipmentButton)
            //    {
            //        ButtonList.Add((EquipmentButton)c);
            //    }
            //}

            EquipmentButtonController buttonController = Controller.Workspace.Resolve<EquipmentButtonController>();
            foreach (EquipmentButton button in ButtonList)
            {
                buttonController.RegisterButton(button);
            }
		}

		protected override void OnLoad(EventArgs e)
		{
            this.nameTextBox.DataBindings.Add("Text", Controller.Model, "Name");
			this.realmTextBox.DataBindings.Add("Text", Controller.Model, "Realm");
			this.classTextBox.DataBindings.Add("Text", Controller.Model, "CharacterClass");

			for (int i = 0; i < Controller.Model.Items.Count(); i++)
			{
				EquipmentButton button = EquipmentButtons.Skip(i).First();
				button.SetModel(Controller.Model.Items.Skip(i).First());
			}
			
			base.OnLoad(e);
		}

		private void loadButton_Click(object sender, EventArgs e)
		{
			Controller.RefreshModel(this.realmTextBox.Text, this.nameTextBox.Text);
		}

	}
}
